//
//  GoumYourPlugin.m
//  Runner
//
//  Created by 九月 on 2023/12/16.
//

#import "GoumYourPlugin.h"
#import "SVProgressHUD.h"
#import <StoreKit/StoreKit.h>

@interface GoumYourPlugin () <FlutterPlugin>

@end

@implementation GoumYourPlugin

+  (instancetype)plugin {
    
    static GoumYourPlugin *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[GoumYourPlugin alloc]init];
    });
    return manager;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    // 注册MethodChannel，指定通信通道名
    FlutterMethodChannel *channel = [FlutterMethodChannel
                                     methodChannelWithName:@"gm_app_channel"
                                           binaryMessenger:[registrar messenger]];
    // 注册方法处理器
    GoumYourPlugin *instance = [[GoumYourPlugin alloc] init];

    [registrar addMethodCallDelegate:instance channel:channel];
}

// 处理Flutter调用的方法
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    if ([call.method isEqualToString:@"requestReview"]) {
        if (@available(iOS 10.3, *)) {
            // 可以使用 SKStoreReviewController
            [SKStoreReviewController requestReview];
        } else {
            // 跳转自定义评分弹窗
            NSURL *appStoreURL = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/6474584711?action=write-review"];
            [[UIApplication sharedApplication] openURL:appStoreURL options:@{} completionHandler:nil];
        }
        result(@"requestReview");
    } 
    else if ([call.method isEqualToString:@"shareApp"]) {
        NSString*textToShare =call.arguments[@"content"];

        UIImage*imageToShare = [UIImage imageNamed:@"logo"];// 本地图片可直接用

        NSURL*urlToShare = [NSURL URLWithString:call.arguments[@"url"]];

        NSArray*activityItems =@[textToShare,imageToShare,urlToShare];

        //    NSArray *activityItems =@[ imageToShare,imageToShare1,urlToShare,textToShare];

        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

        [GoumYourPlugin.plugin.controller presentViewController: activityVC animated:YES completion:nil];
    }
    else {
        result(FlutterMethodNotImplemented);
    }
}

@end
